<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE HTML>
<html lang="en">
<head>
<base href="<%=basePath%>">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/bootstrap.min.css">
<link rel="stylesheet"
	href="${pageContext.request.contextPath}/css/main.css">
<link rel="stylesheet"
	href="//apps.bdimg.com/libs/jqueryui/1.10.4/css/jquery-ui.min.css">
<script src="${pageContext.request.contextPath}/js/jquery.min.js"></script>
<script src="${pageContext.request.contextPath}/js/main.js"></script>
<script src="${pageContext.request.contextPath}/js/jqueryui.js"></script>
<script src="//apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<title>代理商</title>

<script>
	$(function() {
		$("#startDate").datepicker();
		$("#startDate").datepicker("option", "dateFormat", "yy-mm-dd");
	})
</script>
<style>
.main_div {
	min-width: 1000px
}

.center {
	width: 200px;
	height: 100px;
	background-color: #ccc;
	position: absolute;
	left: 50%;
	top: 50%;
	margin-left: -100px;
	margin-top: -50px;
	z-index: 10;
	text-align: center;
	line-height: 50px;
	border-radius: 6px;
}
</style>
</head>
<body>
	<%@ include file="header.jsp"%>
	<%@ include file="left.jsp"%>
	<div id="index"
		style="position: absolute;left: 180px;top: 80px;bottom: 0;right: 0;">
		<section id="motai">
			<div class="scrollbar1-1" id="style1-1">
				<div class="main_div" style="height: 560px;">
					<div class="main_title">
						<span>添加秒杀商品</span>
					</div>
					<form class="form-horizontal" enctype="multipart/form-data"
						style="margin-top: 30px" id="uploadForm">
						<div class="form-group">
							<label class="col-xs-2 control-label">商家名称：</label>
							<div class="col-xs-3">
								<select id="selectOther" class="form-control" name="otherId">

								</select>
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品名称：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="productName"
									name="productName" placeholder="商品名称">
							</div>
							<label class="col-xs-5 control-label" style="text-align: left;color: #ff6c29">*商城首页广告会显示前八个字!</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品原价：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="productPrice"
									name="productPrice" placeholder="请输入商品原价">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">秒杀价格：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="seckillPrice"
									maxlength="6" name="seckillPrice" placeholder="请输入商品秒杀价格">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品库存：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="inventory"
									maxlength="6" name="inventory" placeholder="请输入商品库存">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">秒杀日期：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="startDate"
									name="startDate" placeholder="请输入秒杀日期">
							</div>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">开始时间：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="startTime"
									value="08:00:00" name="startTime" placeholder="请输入活动开始时分秒">
							</div>
							<label class="col-xs-3 control-label">请输入正确时间格式xx:xx:xx，如08:00:00</label>
						</div>

						<div class="form-group">
							<label class="col-xs-2 control-label">结束时间：</label>
							<div class="col-xs-3">
								<input type="text" class="form-control" id="endTime"
									value="23:59:59" name="endTime" placeholder="请输入活动结束时分秒"><input
									id="storeName" name="storeName" style="display:none" />
							</div>
							<label class="col-xs-3 control-label">请输入正确时间格式xx:xx:xx，如23:59:59</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">广告图片：</label>
							<div class="col-xs-3">
								<input type="file" id="publicityFile" name="publicityFile"
									onchange="selectImage2(this);"> <input type="text"
									id="agentId" name="agentId" style="display:none"> <img
									id="image2" style="width:375px;height:150px" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传广告图片（建议大小为750*300像素）</label>
						</div>
						<div class="form-group">
							<label class="col-xs-2 control-label">商品图片：</label>
							<div class="col-xs-3">
								<input type="file" id="productFile" name="productFile"
									onchange="selectImage(this);" /> <img id="image"
									style="width:375px;height:350px" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传商品图片（建议大小为750*750像素）</label>
						</div>
						
						<div class="form-group">
							<label class="col-xs-2 control-label">商品详情图片：</label>
							<div class="col-xs-3">
								<input type="file" id="detailFile" name="detailFile"
									onchange="selectImage3(this);"> <img id="image3"
									style="width:310px;" src="" />
							</div>
							<label class="col-xs-5 control-label"
								style="text-align: left;color: #ff6c29">*必须上传商品详情图片（建议宽度为750像素）</label>
						</div>
						<input style="display:none" value="0" name="typeId" />
						<div class="form-group">
							<label class="col-xs-2 control-label">活动详情：</label>
							<div class="col-xs-4">
								<textarea class="col-xs-12" rows="5" id="details" name="details"></textarea>
							</div>
						</div>
						<div class="col-xs-4 col-xs-offset-2">
							<button id="setButton" class="col-xs-4 btn btn-primary"
								type="button">提交审核</button>
						</div>

					</form>
				</div>
			</div>
		</section>
	</div>
	<script>
		var image = '';
		function selectImage(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage2(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image2').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		function selectImage3(file) { //显示图片
			if (!file.files || !file.files[0]) {
				return;
			}
			var reader = new FileReader();
			reader.onload = function(evt) {
				document.getElementById('image3').src = evt.target.result;
				image = evt.target.result;
			};
			reader.readAsDataURL(file.files[0]);
		}
		$(function() {
			$.post(urls + "seckillAction/getOtherInfo", {
				agentId : localStorage.getItem("agentid"),
				otherStatus : 0
			}, function(res) {
				if (res) {
					$("#selectOther").empty();
					$("#selectOther").append("<option value='-1'>请选择</option>")
					$.each(res, function(n, OtherInfo) {
						$("#selectOther").append("<option value=" + OtherInfo.id + ">" + OtherInfo.otherName + "</option>");
					})
				}
			})
			$("#agentId").val(localStorage.getItem("agentid"))
			$("#setButton").click(function() {
			if($("#productName").val().length<8){
			alert("商品名称长度必须大于8")
			return;
			}
			
				var reg = /^(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
				if (reg.test($("#startTime").val()) && reg.test($("#endTime").val())) {
	
				} else {
					alert("请输入正确的时间，如：12:09:00")
					return;
				}
				var reg2 = /(^[1-9]([0-9]{1,3})?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
				if (reg2.test($("#productPrice").val()) && reg2.test($("#seckillPrice").val())) {
	
				} else {
					alert("请输入正确的价格,或者价格输入过大!")
					return;
				}
				var allinput = $("#uploadForm").find("input");
				var config = true;
				$("#storeName").val($("#selectOther").find("option:selected").text())
				for (var i = 0; i < allinput.length; i++) {
					if (allinput[i].value) {
					} else {
						config = false;
					}
				}
				
				
				if (config) {
					/********************用于判断图片大小以及图片名称是否合法******************/
					var aarr = document.getElementsByTagName("input");
					var barr = [];
					var picreg = /^[A-Za-z0-9\._]{1,100}$/; //图片名称正则
					var flag1 = false; //用于判断图片大小
					var flag2 = false; //用于判断图片名称
					for (var i = 0; i < aarr.length; i++) {
						if (aarr[i].type == "file") {
							barr.push(aarr[i])
						}
					}
					for (var z = 0; z < barr.length; z++) {
						if (barr[z].files[0].size > 200000) { //判断图片大小是否大于200kb
							flag1 = true;
						}
						if (!picreg.test(barr[z].files[0].name)) { //判断图片大小是否大于200kb
							flag2 = true;
						}
					}
					if (flag1) {
						alert("上传图片不得超过200kb每张");
						return;
					}
					if (flag2) {
						alert("图片名称只能由字母与数字组成");
						return;
					}
					/******************用于判断图片大小以及图片名称是否合法****************/
					$.ajax({
						url : urls + "seckillAction/insertSeckillOne",
						type : 'POST',
						cache : false,
						data : new FormData($('#uploadForm')[0]),
						processData : false,
						contentType : false
					}).done(function(res) {
						//console.log(res)
						if (res == true) {
							alert("提交成功")
						} else if (res == false) {
							alert("提交失败")
						} else if (res == 251) {
							alert("当天秒杀广告已存在")
						} else if (res == 250) {
							alert("秒杀广告名称已存在")
						}
					}).fail(function(res) {});
				} else {
					alert("请填写完整信息")
				}
			})
		})
	</script>
</body>
</html>